﻿using Domain;
using Teamwish.Domain.Common;
using Teamwish.Domain.Models.Enums;
using Teamwish.Domain.IBLL;
using Teamwish.Domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace Teamwish.WebUI.Controllers
{
    public class AccountController : Controller
    {
        /// <summary>
        /// 数据库上下文
        /// </summary>
        private EfDbContext db = DIFactory.Resolve<EfDbContext>();

        /// <summary>
        /// 普通顾客登陆
        /// </summary>
        /// <returns></returns>
        public ActionResult Login()
        {
            return View();
        }

        /// <summary>
        /// 处理顾客登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AllowAnonymous]
        public ActionResult Login(string LoginName, string Pwd)
        {
            ViewBag.LoginName = LoginName;
            ViewBag.Pwd = Pwd;
            if (string.IsNullOrWhiteSpace(LoginName))
            {
                TempData["msg"] = "用户名不能为空";
                return View();
            }
            if (string.IsNullOrWhiteSpace(Pwd))
            {
                TempData["msg"] = "密码不能为空";
                return View();
            }

            string pwd = Teamwish.Utilities.Md5Helper.MD5(Pwd.Trim(),32);

            if (db.Userinfo.Any(p=>p.Username==LoginName.Trim()&&p.Password== pwd))
            {
                string roles = "顾客";
                //生成验证票据对象．
                FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(1, LoginName, DateTime.Now, DateTime.Now.AddMinutes(1), false, roles);
                //加密验证票
                string encrytedTicket = FormsAuthentication.Encrypt(authTicket);
                //生成Cookie对象．
                //FormsAuthentication.FormsCookieName取得WebConfig中<Authentication>
                //配置节中Name的值作为Cookie的名字．
                HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrytedTicket);
                Response.Cookies.Add(authCookie);

                return RedirectToAction("Index","Home");
            }
            else
            {
                TempData["msg"] = "用户名或密码错误";
            }

            return View();
        }

        /// <summary>
        /// 顾客注册
        /// </summary>
        /// <returns></returns>
        public ActionResult Register()
        {
            Userinfo userinfo = new Userinfo();
            return View(userinfo);
        }

        /// <summary>
        /// 处理顾客注册
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Register(Userinfo userinfo,string confirmPwd)
        {
            ViewBag.ConfirmPwd = confirmPwd;
            if (string.IsNullOrWhiteSpace(userinfo.Telephone))
            {
                TempData["msg"] = "手机号码要填写";
                return View(userinfo);
            }
            else {
                if (!ArgumentValidation.IsHandset(userinfo.Telephone))
                {
                    TempData["msg"] = "手机号码不正确";
                    return View(userinfo);
                }
            }
            if (string.IsNullOrWhiteSpace(userinfo.Username))
            {
                TempData["msg"] = "请填写用户名";
                return View(userinfo);
            }

            if (db.Userinfo.Any(p => p.Username == userinfo.Username))
            {
                TempData["msg"] = "用户名已经存在";
                return View(userinfo);
            }

            if (string.IsNullOrWhiteSpace(userinfo.Password))
            {
                TempData["msg"] = "请填写密码";
                return View(userinfo);
            }
            if (userinfo.Password!= confirmPwd)
            {
                TempData["msg"] = "两次密码不一致";
                return View(userinfo);
            }
            userinfo.Password = Teamwish.Utilities.Md5Helper.MD5(userinfo.Password,32);
            userinfo.CreateDate = DateTime.Now;
            userinfo.State = (byte)Flag.未审核;

            db.Userinfo.Add(userinfo);
            db.SaveChanges();

            TempData["msg"] = "注册成功";

            return View(userinfo);
        }


        


        public ActionResult LeftNav(string selectNav)
        {
            ViewBag.SelectNav = selectNav;
            return View();
        }

    }
}